


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: DormancyUtils</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.dormancy.util</a> ]
</div>

<h1>Coverage Summary for Class: DormancyUtils (at.dormancy.util)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">DormancyUtils</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    89.3%
  </span>
  <span class="absValue">
    (25/ 28)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    84.5%
  </span>
  <span class="absValue">
    (49/ 58)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2013 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.dormancy.util;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.dormancy.access.AbstractPropertyAccessStrategy;
<i>19</i>&nbsp;import at.dormancy.access.HibernatePropertyAccessStrategy;
<i>20</i>&nbsp;import at.dormancy.access.StrategyPropertyAccessor;
<i>21</i>&nbsp;import at.dormancy.persistence.HibernatePersistenceUnitProvider;
<i>22</i>&nbsp;import org.apache.commons.lang.ArrayUtils;
<i>23</i>&nbsp;import org.apache.log4j.Logger;
<i>24</i>&nbsp;import org.hibernate.*;
<i>25</i>&nbsp;import org.hibernate.collection.spi.PersistentCollection;
<i>26</i>&nbsp;import org.hibernate.internal.AbstractSessionImpl;
<i>27</i>&nbsp;import org.hibernate.metadata.ClassMetadata;
<i>28</i>&nbsp;import org.hibernate.proxy.HibernateProxy;
<i>29</i>&nbsp;import org.hibernate.proxy.LazyInitializer;
<i>30</i>&nbsp;import org.springframework.aop.support.AopUtils;
<i>31</i>&nbsp;import org.springframework.beans.PropertyAccessor;
<i>32</i>&nbsp;import org.springframework.beans.PropertyAccessorFactory;
<i>33</i>&nbsp;import org.springframework.test.util.ReflectionTestUtils;
<i>34</i>&nbsp;import org.springframework.util.ObjectUtils;
<i>35</i>&nbsp;import org.springframework.util.ReflectionUtils;
<i>36</i>&nbsp;
<i>37</i>&nbsp;import javax.annotation.Nonnull;
<i>38</i>&nbsp;import javax.annotation.Nullable;
<i>39</i>&nbsp;import javax.inject.Inject;
<i>40</i>&nbsp;import java.io.Serializable;
<i>41</i>&nbsp;import java.lang.reflect.Field;
<i>42</i>&nbsp;
<i>43</i>&nbsp;/**
<i>44</i>&nbsp; * Utility methods for Dormancy support code.
<i>45</i>&nbsp; *
<i>46</i>&nbsp; * @author Gregor Schauer
<i>47</i>&nbsp; */
<i>48</i>&nbsp;public class DormancyUtils extends AbstractDormancyUtils&lt;SessionFactory, Session, ClassMetadata,
<i>49</i>&nbsp;		HibernatePersistenceUnitProvider&gt; {
<i>50</i>&nbsp;
<i>51</i>&nbsp;	@Inject
<i>52</i>&nbsp;	public DormancyUtils(@Nonnull HibernatePersistenceUnitProvider persistenceUnitProvider) {
<b class="fc"><i>53</i>&nbsp;		super(persistenceUnitProvider);</b>
<b class="fc"><i>54</i>&nbsp;	}</b>
<i>55</i>&nbsp;
<i>56</i>&nbsp;	@Nullable
<i>57</i>&nbsp;	@Override
<i>58</i>&nbsp;	public ClassMetadata getMetadata(@Nullable Class&lt;?&gt; clazz) {
<b class="fc"><i>59</i>&nbsp;		return clazz != null ? persistenceUnitProvider.getMetadata(clazz) : null;</b>
<i>60</i>&nbsp;	}
<i>61</i>&nbsp;
<i>62</i>&nbsp;	/**
<i>63</i>&nbsp;	 * @see ClassMetadata#getMappedClass()
<i>64</i>&nbsp;	 */
<i>65</i>&nbsp;	@Nonnull
<i>66</i>&nbsp;	@Override
<i>67</i>&nbsp;	public Class&lt;?&gt; getMappedClass(@Nonnull ClassMetadata metadata) {
<b class="nc"><i>68</i>&nbsp;		return metadata.getMappedClass();</b>
<i>69</i>&nbsp;	}
<i>70</i>&nbsp;
<i>71</i>&nbsp;	@Nonnull
<i>72</i>&nbsp;	@Override
<i>73</i>&nbsp;	public String getEntityName(@Nonnull Class&lt;?&gt; clazz) {
<b class="fc"><i>74</i>&nbsp;		ClassMetadata metadata = getMetadata(clazz);</b>
<b class="fc"><i>75</i>&nbsp;		return metadata != null ? metadata.getEntityName() : clazz.getSimpleName();</b>
<i>76</i>&nbsp;	}
<i>77</i>&nbsp;
<i>78</i>&nbsp;	@Nullable
<i>79</i>&nbsp;	@Override
<i>80</i>&nbsp;	protected String getIdentifierPropertyName(@Nonnull Class&lt;?&gt; clazz) {
<b class="fc"><i>81</i>&nbsp;		ClassMetadata metadata = getMetadata(clazz);</b>
<b class="fc"><i>82</i>&nbsp;		return metadata != null ? metadata.getIdentifierPropertyName() : null;</b>
<i>83</i>&nbsp;	}
<i>84</i>&nbsp;
<i>85</i>&nbsp;	@Override
<i>86</i>&nbsp;	@Nullable
<i>87</i>&nbsp;	public &lt;T&gt; Serializable getIdentifier(@Nonnull ClassMetadata metadata, @Nonnull T bean) {
<b class="fc"><i>88</i>&nbsp;		Serializable identifier = metadata.getIdentifier(bean, (AbstractSessionImpl) getPersistenceContext());</b>
<b class="fc"><i>89</i>&nbsp;		if (identifier == null) {</b>
<b class="fc"><i>90</i>&nbsp;			identifier = Serializable.class.cast(getPropertyValue(metadata, bean, metadata.getIdentifierPropertyName()));</b>
<i>91</i>&nbsp;		}
<b class="fc"><i>92</i>&nbsp;		return identifier;</b>
<i>93</i>&nbsp;	}
<i>94</i>&nbsp;
<i>95</i>&nbsp;	@Override
<i>96</i>&nbsp;	public boolean isVersioned(@Nonnull ClassMetadata metadata) {
<b class="fc"><i>97</i>&nbsp;		return metadata.isVersioned();</b>
<i>98</i>&nbsp;	}
<i>99</i>&nbsp;
<i>100</i>&nbsp;	@Nullable
<i>101</i>&nbsp;	@Override
<i>102</i>&nbsp;	public String getVersionPropertyName(@Nonnull ClassMetadata metadata) {
<b class="fc"><i>103</i>&nbsp;		int index = metadata.getVersionProperty();</b>
<b class="fc"><i>104</i>&nbsp;		return index &gt;= 0 ? metadata.getPropertyNames()[index] : null;</b>
<i>105</i>&nbsp;	}
<i>106</i>&nbsp;
<i>107</i>&nbsp;	@Override
<i>108</i>&nbsp;	@Nullable
<i>109</i>&nbsp;	public Object getPropertyValue(@Nullable ClassMetadata metadata, @Nonnull Object bean, @Nonnull String propertyName) {
<b class="fc"><i>110</i>&nbsp;		return IntrospectorUtils.getValue(bean, propertyName);</b>
<i>111</i>&nbsp;	}
<i>112</i>&nbsp;
<i>113</i>&nbsp;	@Override
<i>114</i>&nbsp;	public void setPropertyValue(@Nullable ClassMetadata metadata, @Nonnull Object bean, @Nonnull String propertyName, @Nullable Object value) {
<b class="fc"><i>115</i>&nbsp;		IntrospectorUtils.setValue(bean, propertyName, value);</b>
<b class="fc"><i>116</i>&nbsp;	}</b>
<i>117</i>&nbsp;
<i>118</i>&nbsp;	@Nonnull
<i>119</i>&nbsp;	@Override
<i>120</i>&nbsp;	public Class&lt;?&gt; getPropertyType(@Nonnull Class&lt;?&gt; clazz, @Nonnull String propertyName) {
<b class="fc"><i>121</i>&nbsp;		ClassMetadata metadata = getMetadata(clazz);</b>
<b class="fc"><i>122</i>&nbsp;		if (metadata != null) {</b>
<i>123</i>&nbsp;			try {
<b class="fc"><i>124</i>&nbsp;				return metadata.getPropertyType(propertyName).getReturnedClass();</b>
<b class="fc"><i>125</i>&nbsp;			} catch (QueryException e) {</b>
<b class="fc"><i>126</i>&nbsp;				if (ArrayUtils.contains(metadata.getPropertyNames(), propertyName)) {</b>
<b class="nc"><i>127</i>&nbsp;					throw e;</b>
<i>128</i>&nbsp;				}
<i>129</i>&nbsp;			}
<i>130</i>&nbsp;		}
<b class="fc"><i>131</i>&nbsp;		return ReflectionUtils.findField(clazz, propertyName).getType();</b>
<i>132</i>&nbsp;	}
<i>133</i>&nbsp;
<i>134</i>&nbsp;	@Nullable
<i>135</i>&nbsp;	@Override
<i>136</i>&nbsp;	public PropertyAccessor getPropertyAccessor(@Nullable ClassMetadata metadata, @Nonnull Object obj) {
<b class="fc"><i>137</i>&nbsp;		if (metadata == null) {</b>
<b class="fc"><i>138</i>&nbsp;			return PropertyAccessorFactory.forDirectFieldAccess(obj);</b>
<i>139</i>&nbsp;		}
<b class="fc"><i>140</i>&nbsp;		if (isJavassistProxy(obj.getClass())) {</b>
<i>141</i>&nbsp;			// If the object is a proxy, attempt to find its LazyInitializer
<b class="fc"><i>142</i>&nbsp;			Field handlerField = ReflectionUtils.findField(obj.getClass(), &quot;handler&quot;);</b>
<b class="fc"><i>143</i>&nbsp;			if (handlerField != null) {</b>
<i>144</i>&nbsp;				// If the LazyInitializer is available, obtain the underlying object
<b class="fc"><i>145</i>&nbsp;				LazyInitializer lazyInitializer = (LazyInitializer) ReflectionTestUtils.getField(obj, &quot;handler&quot;);</b>
<i>146</i>&nbsp;				// Initialize it if necessary and return a DirectFieldAccessor for the nested object
<b class="fc"><i>147</i>&nbsp;				obj = lazyInitializer.getImplementation();</b>
<b class="fc"><i>148</i>&nbsp;			} else {</b>
<i>149</i>&nbsp;				/*
<i>150</i>&nbsp;				 * Otherwise log a warning message because this is very unlikely to happen or even impossible.
<i>151</i>&nbsp;				 * However, instead of throwing an exception, property access is used as a fallback solution.
<i>152</i>&nbsp;				 */
<b class="nc"><i>153</i>&nbsp;				Logger.getLogger(getClass()).warn(&quot;Cannot retrieve field named &#39;handler&#39; of type &#39;org.hibernate.proxy.LazyInitializer&#39; from &quot; + ObjectUtils.identityToString(obj));</b>
<i>154</i>&nbsp;			}
<i>155</i>&nbsp;		}
<b class="fc"><i>156</i>&nbsp;		return new StrategyPropertyAccessor(obj, getPropertyAccessStrategy(AopUtils.getTargetClass(obj)));</b>
<i>157</i>&nbsp;	}
<i>158</i>&nbsp;
<i>159</i>&nbsp;	@Nonnull
<i>160</i>&nbsp;	@Override
<i>161</i>&nbsp;	protected AbstractPropertyAccessStrategy createStrategy(@Nonnull Class&lt;?&gt; clazz) {
<b class="fc"><i>162</i>&nbsp;		return new HibernatePropertyAccessStrategy(clazz);</b>
<i>163</i>&nbsp;	}
<i>164</i>&nbsp;
<i>165</i>&nbsp;	@Nullable
<i>166</i>&nbsp;	@Override
<i>167</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>168</i>&nbsp;	public &lt;T&gt; T find(@Nonnull Class&lt;T&gt; clazz, @Nonnull Serializable id) {
<b class="fc"><i>169</i>&nbsp;		return (T) getPersistenceContext().get(clazz, id);</b>
<i>170</i>&nbsp;	}
<i>171</i>&nbsp;
<i>172</i>&nbsp;	@Override
<i>173</i>&nbsp;	public void flush() {
<b class="nc"><i>174</i>&nbsp;		getPersistenceContext().flush();</b>
<b class="nc"><i>175</i>&nbsp;	}</b>
<i>176</i>&nbsp;
<i>177</i>&nbsp;	@Nonnull
<i>178</i>&nbsp;	@Override
<i>179</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>180</i>&nbsp;	public &lt;K&gt; K persist(@Nonnull Object obj) {
<b class="fc"><i>181</i>&nbsp;		return (K) getPersistenceContext().save(obj);</b>
<i>182</i>&nbsp;	}
<i>183</i>&nbsp;
<i>184</i>&nbsp;	/**
<i>185</i>&nbsp;	 * @see #isInitialized(Object)
<i>186</i>&nbsp;	 * @see HibernateProxy
<i>187</i>&nbsp;	 */
<i>188</i>&nbsp;	@Override
<i>189</i>&nbsp;	public boolean isProxy(@Nullable Object obj) {
<b class="fc"><i>190</i>&nbsp;		return obj instanceof HibernateProxy;</b>
<i>191</i>&nbsp;	}
<i>192</i>&nbsp;
<i>193</i>&nbsp;	/**
<i>194</i>&nbsp;	 * @see Hibernate#isInitialized(Object)
<i>195</i>&nbsp;	 */
<i>196</i>&nbsp;	@Override
<i>197</i>&nbsp;	public boolean isInitialized(@Nullable Object obj) {
<b class="fc"><i>198</i>&nbsp;		return Hibernate.isInitialized(obj);</b>
<i>199</i>&nbsp;	}
<i>200</i>&nbsp;
<i>201</i>&nbsp;	@Override
<i>202</i>&nbsp;	public boolean isUninitialized(@Nonnull String propertyName, @Nonnull Object dbObj, @Nonnull Object dbValue, @Nullable Object trValue) {
<b class="fc"><i>203</i>&nbsp;		HibernateProxy hibernateProxy = HibernateProxy.class.cast(dbValue);</b>
<b class="fc"><i>204</i>&nbsp;		LazyInitializer lazyInitializer = hibernateProxy.getHibernateLazyInitializer();</b>
<b class="fc"><i>205</i>&nbsp;		if (lazyInitializer.isUninitialized()) {</b>
<i>206</i>&nbsp;			// If property is loaded lazily, the value of the given object must be null
<b class="nc"><i>207</i>&nbsp;			if (trValue != null) {</b>
<b class="nc"><i>208</i>&nbsp;				throw throwLazyPropertyNotNullException(trValue, dbObj, propertyName);</b>
<i>209</i>&nbsp;			}
<b class="nc"><i>210</i>&nbsp;			return true;</b>
<i>211</i>&nbsp;		}
<b class="fc"><i>212</i>&nbsp;		return false;</b>
<i>213</i>&nbsp;	}
<i>214</i>&nbsp;
<i>215</i>&nbsp;	@Override
<i>216</i>&nbsp;	public boolean isPersistentCollection(@Nullable Object obj) {
<b class="fc"><i>217</i>&nbsp;		return obj instanceof PersistentCollection;</b>
<i>218</i>&nbsp;	}
<i>219</i>&nbsp;
<i>220</i>&nbsp;	/**
<i>221</i>&nbsp;	 * @see PersistentCollection#wasInitialized()
<i>222</i>&nbsp;	 */
<i>223</i>&nbsp;	@Override
<i>224</i>&nbsp;	public boolean isInitializedPersistentCollection(@Nullable Object obj) {
<b class="fc"><i>225</i>&nbsp;		return isPersistentCollection(obj) &amp;&amp; PersistentCollection.class.cast(obj).wasInitialized();</b>
<i>226</i>&nbsp;	}
<i>227</i>&nbsp;
<i>228</i>&nbsp;	/**
<i>229</i>&nbsp;	 * Throws a {@link TransientObjectException} indicating that the object has no valid identifier.
<i>230</i>&nbsp;	 *
<i>231</i>&nbsp;	 * @throws TransientObjectException the exception
<i>232</i>&nbsp;	 */
<i>233</i>&nbsp;	@Override
<i>234</i>&nbsp;	public RuntimeException throwNullIdentifierException(@Nonnull Object object) {
<b class="fc"><i>235</i>&nbsp;		throw new TransientObjectException(&quot;The given object has a null identifier: &quot; + getEntityName(getClass(object)));</b>
<i>236</i>&nbsp;	}
<i>237</i>&nbsp;
<i>238</i>&nbsp;	/**
<i>239</i>&nbsp;	 * Throws a {@link TransientObjectException} indicating that the object must be saved manually before continuing.
<i>240</i>&nbsp;	 *
<i>241</i>&nbsp;	 * @throws TransientObjectException the exception
<i>242</i>&nbsp;	 */
<i>243</i>&nbsp;	@Override
<i>244</i>&nbsp;	public RuntimeException throwUnsavedTransientInstanceException(@Nonnull Object object) {
<b class="nc"><i>245</i>&nbsp;		throw new TransientObjectException(</b>
<i>246</i>&nbsp;				&quot;object references an unsaved transient instance - save the transient instance before flushing: &quot; +
<i>247</i>&nbsp;						getEntityName(getClass(object))
<i>248</i>&nbsp;		);
<i>249</i>&nbsp;	}
<i>250</i>&nbsp;
<i>251</i>&nbsp;	/**
<i>252</i>&nbsp;	 * Throws a {@link PropertyValueException} when a lazy property holds a value while {@code null} was expected.
<i>253</i>&nbsp;	 *
<i>254</i>&nbsp;	 * @throws PropertyValueException the exception
<i>255</i>&nbsp;	 */
<i>256</i>&nbsp;	@Override
<i>257</i>&nbsp;	public RuntimeException throwLazyPropertyNotNullException(@Nonnull Object trValue, @Nonnull Object dbObj, @Nonnull String propertyName) {
<b class="fc"><i>258</i>&nbsp;		throw new PropertyValueException(&quot;Property is loaded lazily. Therefore it must be null but was: &quot; + trValue,</b>
<i>259</i>&nbsp;				getEntityName(getClass(dbObj)), propertyName);
<i>260</i>&nbsp;	}
<i>261</i>&nbsp;
<i>262</i>&nbsp;	/**
<i>263</i>&nbsp;	 * Throws a {@link StaleObjectStateException} when an optimistic locking conflict occurs.
<i>264</i>&nbsp;	 *
<i>265</i>&nbsp;	 * @throws StaleObjectStateException the exception
<i>266</i>&nbsp;	 */
<i>267</i>&nbsp;	@Override
<i>268</i>&nbsp;	public RuntimeException throwOptimisticLockException(@Nonnull Object dbValue, @Nonnull Serializable identifier) {
<b class="fc"><i>269</i>&nbsp;		throw new StaleObjectStateException(getEntityName(getClass(dbValue)), identifier);</b>
<i>270</i>&nbsp;	}
<i>271</i>&nbsp;
<i>272</i>&nbsp;	/**
<i>273</i>&nbsp;	 * Throws an {@link ObjectNotFoundException} when an entity reference is accessed but the entity does not exist.
<i>274</i>&nbsp;	 *
<i>275</i>&nbsp;	 * @throws ObjectNotFoundException the exception
<i>276</i>&nbsp;	 */
<i>277</i>&nbsp;	@Override
<i>278</i>&nbsp;	public RuntimeException throwEntityNotFoundException(@Nonnull Serializable identifier, @Nonnull Object trObj) {
<b class="fc"><i>279</i>&nbsp;		throw new ObjectNotFoundException(identifier, getClass(trObj).getSimpleName());</b>
<i>280</i>&nbsp;	}
<i>281</i>&nbsp;
<i>282</i>&nbsp;	/**
<i>283</i>&nbsp;	 * Throws a {@link PropertyValueException} when a property is not accessible or holds an unexpected value.
<i>284</i>&nbsp;	 *
<i>285</i>&nbsp;	 * @throws PropertyValueException the exception
<i>286</i>&nbsp;	 */
<i>287</i>&nbsp;	@Override
<i>288</i>&nbsp;	public RuntimeException throwPropertyValueException(@Nonnull String message, @Nonnull Object bean) {
<b class="fc"><i>289</i>&nbsp;		throw new PropertyValueException(message, getEntityName(bean.getClass()), getIdentifierPropertyName(bean.getClass()));</b>
<i>290</i>&nbsp;	}
<i>291</i>&nbsp;
<i>292</i>&nbsp;	@Nonnull
<i>293</i>&nbsp;	@Override
<i>294</i>&nbsp;	public Session getPersistenceContext() {
<b class="fc"><i>295</i>&nbsp;		return persistenceUnitProvider.getPersistenceContextProvider().getPersistenceContext();</b>
<i>296</i>&nbsp;	}
<i>297</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2013-07-31 23:31</div>
</div>
</body>
</html>
